package jwt_helpers

import (
	"github.com/gofiber/fiber/v2"
	"github.com/golang-jwt/jwt/v4"
	"notabug.org/alimiracle/my-website-api/models"
	"time"
)

func Create_token(user models.User) (string, error) {

	// Create the Claims
	claims := jwt.MapClaims{
		"name":  user.Name,
		"admin": user.Is_admin,
		"exp":   time.Now().Add(time.Hour * 72).Unix(),
	}

	// Create token
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

	// Generate encoded token
	t, err := token.SignedString([]byte("secret"))
	return t, err
}
func IS_user_admin(c *fiber.Ctx) bool {
	jwt_user := c.Locals("user").(*jwt.Token)
	claims := jwt_user.Claims.(jwt.MapClaims)
	is_admin := claims["admin"].(bool)
	return is_admin
}
